//
//  PhotoAlbumsTableViewController.swift
//  selectPhoto
//
//  Created by 孟祥群 on 2018/4/24.
//  Copyright © 2018年 孟祥群. All rights reserved.
//

import UIKit
import Photos
class PhotoAlbumsTableViewController: UITableViewController, PHPhotoLibraryChangeObserver {
    // 自定义需要加载的相册
    var customSmartCollections = [
        PHAssetCollectionSubtype.smartAlbumUserLibrary, // All Photos
        PHAssetCollectionSubtype.smartAlbumRecentlyAdded // Rencent Added
    ]
    // tableCellIndetifier
    let albumTableViewCellItentifier = "PhotoAlbumTableViewCell"
    //数据源
    var albums:Array = [ImageModel]()
    
    let imageManager = PHImageManager.default()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
        
        // 9.0以上添加截屏图片
        if #available(iOS 9.0, *) {
            customSmartCollections.append(.smartAlbumScreenshots)
        }
        //注册通知
        PHPhotoLibrary.shared().register(self)
        
        self.setupTableView()
        self.configNavigationBar()
        
         self.loadAlbums(replace: false)
    }
    
    //监听相册发生变化
    func photoLibraryDidChange(_ changeInstance: PHChange) {
        self.loadAlbums(replace: true)
    }
    
    private func configNavigationBar(){
        let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(PhotoAlbumsTableViewController.eventViewControllerDismiss))
        self.navigationItem.rightBarButtonItem = cancelButton
    }
    @objc func eventViewControllerDismiss(){
        PhotoImage.instance.selectedImage.removeAll()
        self.navigationController?.dismiss(animated: true, completion: nil)
    }
    private func setupTableView(){
        self.tableView.register(UINib.init(nibName: self.albumTableViewCellItentifier, bundle: nil), forCellReuseIdentifier: self.albumTableViewCellItentifier)
        
        // 自定义 separatorLine样式
        self.tableView.rowHeight = PhotoPickerConfig.AlbumTableViewCellHeight
        self.tableView.separatorColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.15)
        self.tableView.separatorInset = UIEdgeInsets.zero
        
        // 去除tableView多余空格线
        self.tableView.tableFooterView = UIView.init(frame: CGRect.zero)
    }
    
    
    /// 获取数据源
    ///
    /// - Parameter replace: 相册是否发生变化
    private func loadAlbums(replace: Bool){
        if replace {
            self.albums.removeAll()
        }
        // 加载Smart Albumns All Photos
        let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: nil)
        for i in 0 ..< smartAlbums.count  {
            if customSmartCollections.contains(smartAlbums[i].assetCollectionSubtype){
                self.filterFetchResult(collection: smartAlbums[i])
            }
        }
        
        // 用户相册
        let topUserLibarayList = PHCollectionList.fetchTopLevelUserCollections(with: nil)
        for i in 0 ..< topUserLibarayList.count {
            if let topUserAlbumItem = topUserLibarayList[i] as? PHAssetCollection {
                self.filterFetchResult(collection: topUserAlbumItem)
            }
        }
        self.tableView.reloadData()
    }
    
    private func filterFetchResult(collection: PHAssetCollection){
        let fetchResult = PHAsset.fetchAssets(in: collection, options: PhotoFetchOptions.shareInstance)
        if fetchResult.count > 0 {
            let model = ImageModel(result: fetchResult as! PHFetchResult<AnyObject> as! PHFetchResult<PHObject>, label: collection.localizedTitle, assetType: collection.assetCollectionSubtype)
            self.albums.append(model)
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.albums.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = PhotoAlbumTableViewCell(style: .default, reuseIdentifier: self.albumTableViewCellItentifier)
        let model = self.albums[indexPath.row]
        cell.renderData(result: model.fetchResult as! PHFetchResult<AnyObject>, label: model.label)
        cell.accessoryType = .disclosureIndicator

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.showDetailPageModel(model: self.albums[indexPath.row])
    }
    
    
    private func showDetailPageModel(model: ImageModel){
        let layout = PhotoCollectionViewController.configCustomCollectionLayout()
        let controller = PhotoCollectionViewController(collectionViewLayout: layout)
        
        controller.fetchResult = model.fetchResult
        self.navigationController?.show(controller, sender: nil)
    }
    
    deinit {
        PHPhotoLibrary.shared().unregisterChangeObserver(self)
    }
  
}
